<?php

namespace App\Services\Form;

use App\Services\Api\Api;

class initFormData
{

    /**
     * 专家列表
     * @param  [type]
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getFilterIndustryExpertCoo($request, $currentTabMenuList, $apiCode)
    {
        $type = $request->input('type');
        if (!isset($currentTabMenuList[$type])) {
            $type = 1;
        }
        $clazz = $currentTabMenuList[$type]['java_flag_type'];
        $formData = [
            'pageNo' => $request->input('page', 1),
            'pageSize' => $request->input('limit', 2),
            'clazz' => $clazz,
            'areaId' => $request->input('area', ''),
        ];

        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['specialists'],
                'pageCount' => $result['pageTotal'],
                'filterType' => $type,
            ];
        }
        return false;
    }

    /**
     * 供应链服务列表
     * @param  [type]
     * @param  string
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getFilterIndustryChainCoo($request, $otherConfig = '', $currentTabMenuList, $apiCode)
    {

        $type = $request->input('type');
        if (!isset($currentTabMenuList[$type])) {
            $type = 1;
        }
        $type = isset($otherConfig['type']) ? $otherConfig['type'] : $type;
        $domainId = $currentTabMenuList[$type]['java_flag_type'];
        $formData = [
            'pageNo' => $request->input('page', 1),
            'pageSize' => $request->input('limit', isset($otherConfig['limit']) ? $otherConfig['limit'] : 2),
            'title' => $request->input('title', ''),
            'techRoute' => $request->input('tech_checkbox', -1),
            'domainId' => $domainId,
            'typeId' => 2,
        ];

        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['dataList'],
                'pageCount' => $result['pageTotal'],
                'filterType' => $type,
            ];
        }
        return false;
    }

    /**
     * 获取碳银研究院政策和行业研究报告文章列表
     * @param
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getFilterTanwinLib($request, $currentTabMenuList, $apiCode)
    {
        $type = $request->input('type');
        if (!isset($currentTabMenuList[$type])) {
            $type = 1;
        }
        $categoryId = $currentTabMenuList[$type]['java_flag_type'];

        $from = $request->input('tech_checkbox');
        if ($from != -1 && $from) {
            $from = explode(',', $from);
        } else {
            $from = [];
        }
        $formData = [
            'pageNo' => $request->input('page', 1),
            'pageSize' => $request->input('limit', 10),
            'title' => $request->input('title', ''),
            'domain' => $request->input('domain', ''),
            'from' => $from,
            'categoryId' => $categoryId,
        ];
        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['articles'],
                'pageCount' => $result['pageTotal'],
                'filterType' => $type,
            ];
        }
        return false;
    }

    /**
     * 资产库列表
     * @param  [type]
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getFilterAssetCoo($request, $currentTabMenuList, $apiCode)
    {
        $type = $request->input('type');
        if (!isset($currentTabMenuList[$type])) {
            $type = 1;
        }
        //$type = $currentTabMenuList[$requestData['filter_type']]['java_flag_type'];//这里采用了两个接口，无需通过类别来判断

        //找项目
        if ($type == 1) {
            $formData = [
                'pageNo' => $request->input('page', 1),
                'pageSize' => $request->input('limit', 2),
                'serverType' => $request->input('project_type', ''),
                'financingLimitType' => $request->input('fina_limit', ''),
                'financingTermType' => $request->input('fina_time', ''),
                'financingCostType' => $request->input('fina_cost', ''),
            ];
            $obj = new Api();
            $postData = $obj->getJavaPostData($apiCode, $formData);
            $result = $obj->getJavaResponseData($postData);
            if ($result) {
                return [
                    'dataList' => $result['dataList'],
                    'pageCount' => $result['pageTotal'],
                    'filterType' => $type,
                ];
            }
            return false;
        }
        //找资金
        else if ($type == 2) {
            $finaTime = $request->input('fina_time');
            if ($finaTime == 1) {
                $finaTime = '1-1';
            } else if ($finaTime == 2) {
                $finaTime = '2-3';
            } else if ($finaTime == 3) {
                $finaTime = '4-10';
            } else {
                $finaTime = '';
            }
            $formData = [
                'pageNo' => $request->input('page', 1),
                'pageSize' => $request->input('limit', 2),
                'domainId' => $request->input('project_type', -1),
                'mainId' => $request->input('fina_main', -1),
                'methodId' => $request->input('fina_method', -1),
                'limitYear' => $finaTime,
            ];

            $obj = new Api();
            $postData = $obj->getJavaPostData($apiCode, $formData);
            $result = $obj->getJavaResponseData($postData);
            if ($result) {
                return [
                    'dataList' => $result['dataList'],
                    'pageCount' => $result['pageTotal'],
                    'filterType' => $type,
                ];
            }
            return false;
        }
    }

    /**
     * 市场合作-技术合作文章分类列表
     * @param  [type]
     * @return [type]
     */
    public function getFilterMarketTechList($request, $apiCode)
    {
        $categoryId = config('front.tab_menu.market_tech.1.java_flag_type');
        $formData = [
            'pageNo' => $request->input('page', 1),
            'pageSize' => $request->input('limit', 2),
            'title' => $request->input('title', ''),
            'categoryId' => $categoryId,
        ];
        $obj = new Api();
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['articles'],
                'pageCount' => $result['pageTotal'],
            ];
        }
        return false;
    }

    /**
     * 获取新闻列表
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getNewsList($request, $apiCode)
    {

        $obj = new Api();
        $formData = [
            'pageNo' => $request->input('page', 1),
            'pageSize' => $request->input('limit', 5),
            'categoryId' => 1,
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['articles'],
                'pageCount' => $result['pageTotal'],
            ];
        }
        return false;
    }

    /**
     * 获取新闻详情
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getNewsInfo($request, $apiCode)
    {
        $obj = new Api();
        $formData = [
            'id' => $request->input('id'),
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'newsInfo' => $result['cmsZArticle'],
            ];
        }
        return false;
    }

    /**
     * 获取其他详情
     * @param
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function getOtherInfo($request, $type, $apiCode)
    {
        $obj = new Api();
        $formData = [
            'type' => $type,
            'id' => $request->input('id'),
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataInfo' => $result,
            ];
        }
        return false;
    }

    public function getFilterList($request, $apiCode)
    {
        $obj = new Api();
        $formData = [
            'name' => '',
            'areaId' => -1,
            'domainId' => -1,
            'techRouteId' => -1,
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'areaList' => $result['conditions']['area']['children'],
                'domainList' => $result['conditions']['domain']['children'],
            ];
        }
        return false;
    }

    public function getDomainTechRouteList($request, $apiCode, $domainList)
    {
        $domainId = $request->input('domain', '');
        if ($domainId && $domainId != -1) {
            $domainIdList = [$domainId];
        } else {
            if ($domainList) {
                foreach ($domainList as $key => $value) {
                    $domainIdList[] = $value['id'];
                }
            }
        }

        $obj = new Api();

        $formData = [
            'domainIds' => $domainList ? implode(',', $domainIdList) : '',
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'techRouteList' => $result['list'],
            ];
        }
        return false;
    }

    public function getTechRouteFilterList($request, $apiCode)
    {
        $obj = new Api();
        $formData = [];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'techRouteFilterList' => $result['conditions']['techRoute']['children'],
            ];
        }
        return false;
    }
}
